package com.android.rapidcash.activitys

import android.Manifest
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.widget.Toast
import com.android.lib.base.baseclass.BaseApplication
import com.android.lib.base.baseclass.BaseVMActivity
import com.android.lib.base.ext.AesUtils
import com.android.lib.base.ext.chip
import com.android.lib.base.ext.click
import com.android.lib.base.ext.toAes
import com.android.rapidcash.MainActivity
import com.android.rapidcash.viewmodel.MainViewModel
import com.arc.fast.span.appendFastTextStyle
import com.arc.fast.span.enableClickableSpan
import com.blankj.utilcode.util.LogUtils
import com.permissionx.guolindev.PermissionX
import com.permissionx.guolindev.request.ExplainScope
import com.rapidcash.abanca.santander.vivus.revolut.waylet.libertex.icmarkets.caixabank.R
import com.rapidcash.abanca.santander.vivus.revolut.waylet.libertex.icmarkets.caixabank.databinding.ActivityPermissionBinding


class PermissionActivity : BaseVMActivity<MainViewModel, ActivityPermissionBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.tvIcon.chip(6)
        mBinding.btCancel.chip(25)
    }

    override fun initData() {
        val s1 = AesUtils.encrypt(getString(R.string.per_info))
        val s2 = AesUtils.encrypt(getString(R.string.per_info2))
        LogUtils.e(
            s1, s2
        )
        LogUtils.e(
            s1.toAes()
        )

        val spannableStringBuilder = SpannableStringBuilder()

        spannableStringBuilder.append(getString(R.string.per_info) + " ")

        spannableStringBuilder.appendFastTextStyle(
            "<${getString(R.string.per_info2)}>"
        ) {
            textColor = Color.parseColor("#FE5A59")
            onClick = {
                startActivity(Intent(BaseApplication.getContext(), WebActivity::class.java).apply {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    putExtra("url", "https://www.baidu.com")
                    putExtra("title", getString(R.string.per_info2))
                })
            }
        }
        mBinding.tvInfo.text = spannableStringBuilder
        mBinding.tvInfo.enableClickableSpan() // 启用点击事件
    }

    override fun initListener() {
        mBinding.btAccpect.click {
            bestPermissionX()
        }
        mBinding.btCancel.click {
            goMain()
        }
    }

    private fun goMain() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }


    private fun bestPermissionX() {
        PermissionX.init(this)
            .permissions(
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.ACCESS_COARSE_LOCATION
            )
            .setDialogTintColor(Color.parseColor("#008577"), Color.parseColor("#83e8dd"))
            .onExplainRequestReason { scope: ExplainScope, deniedList: List<String> ->
                goMain()
            }
            .request { allGranted: Boolean, grantedList: List<String?>?, deniedList: List<String?> ->
                goMain()
            }
    }


}